package cn.explink.dop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.vo.DeliveryManVO;
import cn.explink.dop.enums.DeliveryManEnum;
import cn.explink.dop.enums.DeliveryManStatusEnum;
import cn.explink.dop.service.AreaService;
import cn.explink.dop.service.DeliveryManService;
import cn.explink.dop.service.StorefrontService;
//import cn.explink.dop.tools.ImageCompressUtil;
import cn.explink.eap.core.common.model.json.AjaxJson;
import cn.explink.eap.web.cgform.controller.autolist.CgAutoListController;
import cn.explink.eap.web.system.pojo.base.TSUser;

/**
 * 业务员信息控制器
 *
 * @author firefly 2015-2-28 11:07
 *
 */
@Controller
@RequestMapping("/deliveryManController")
public class DeliveryManController extends CgAutoListController {

	@Autowired
	private DeliveryManService deliveryManService;

	@Autowired
	private AreaService areaService;

	@Autowired
	private StorefrontService storefrontService;

	/**
	 * 基本信息
	 *
	 * @param request
	 *            请求
	 * @return 基本信息页面
	 */
	@RequestMapping(params = "index")
	public ModelAndView storefrontIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dop/deliveryman/deliveryman");
		return view;
	}

	@RequestMapping(params = "create")
	@ResponseBody
	public AjaxJson saveBaseInfo(String delName, String delCertificates, String delPortrait, String delTeliPhone, String delIntroduction, String sUser, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			DeliveryManVO del = new DeliveryManVO();
			del.setDelName(delName);
			del.setStatus(DeliveryManStatusEnum.CAN_ACCEPT.getValue());
			del.setDelCertificates(delCertificates);
			del.setDelPortrait(delPortrait);
			del.setDelTelephone(delTeliPhone);
			del.setDelIntroduction(delIntroduction);
			del.setDelMaxmum(0);
			TSUser tsUser = new TSUser();
			tsUser.setId(sUser);
			del.setsUser(tsUser);
			del.setDelEnabled(DeliveryManEnum.Enable.getValue());
			String id = this.deliveryManService.add(del);
			if (null != id) {
				j.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@RequestMapping(params = "modify")
	@ResponseBody
	public AjaxJson updateData(String id, String delName, String delCertificates, String delPortrait, String delTeliPhone, String delIntroduction, String sUser, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			DeliveryManVO del = new DeliveryManVO();
			del.setId(id);
			del.setDelCertificates(delCertificates);
			del.setDelPortrait(delPortrait);
			del.setDelTelephone(delTeliPhone);
			del.setDelIntroduction(delIntroduction);
			del.setDelMaxmum(0);
			TSUser tsUser = new TSUser();
			tsUser.setId(sUser);
			del.setsUser(tsUser);
			this.deliveryManService.modify(del);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 获取数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "loadData")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<DeliveryManVO> loadData(int pageNum, int pageSize, HttpServletRequest request) {
		try {
			Map<String, Object> map = this.deliveryManService.getListDataByPage(new DeliveryManVO(), pageNum, pageSize);
			return (List<DeliveryManVO>) map.get("list");
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<DeliveryManVO>();
		}
	}

	/**
	 * 获取数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "findData")
	@ResponseBody
	public List<DeliveryManVO> findData(HttpServletRequest request) {
		try {
			return this.deliveryManService.getListData(new DeliveryManVO());
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<DeliveryManVO>();
		}
	}

	@RequestMapping(params = "findDataByProperty")
	@ResponseBody
	public List<DeliveryManVO> findDataByProperty(Boolean havingpos, Boolean havingwarmbox, HttpServletRequest request) {
		try {
			DeliveryManVO deliveryManVO = new DeliveryManVO();
			if (havingpos) {
				deliveryManVO.setHavingPOS(1);
			} else {
				deliveryManVO.setHavingPOS(-1);
			}

			if (havingwarmbox) {
				deliveryManVO.setHavingWarmBox(1);
			} else {
				deliveryManVO.setHavingWarmBox(-1);
			}
			deliveryManVO.setIsDeliveryMan(1);
			return this.deliveryManService.getListData(deliveryManVO);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<DeliveryManVO>();
		}
	}

	/**
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getById")
	@ResponseBody
	public DeliveryManVO getById(String id, HttpServletRequest request) {
		try {
			DeliveryManVO deliveryManVO = this.deliveryManService.queryById(id);
			return deliveryManVO;
		} catch (Exception e) {
			e.printStackTrace();
			return new DeliveryManVO();
		}
	}

	/**
	 * 获取查询数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "queryList")
	@ResponseBody
	public Map<String, Object> queryData(DeliveryManVO del, int pageNum, int pageSize, HttpServletRequest request) {
		try {
			return this.deliveryManService.getListDataByPage(del, pageNum, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap<String, Object>();
		}
	}

	/**
	 * 删除数据
	 *
	 * @param rowList
	 *            数据字符串
	 * @param request
	 *            求情
	 * @return 删除状态
	 */
	@RequestMapping(params = "delete")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public AjaxJson deleteRows(String rowList, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			JSONArray json = JSONArray.fromObject(rowList);
			List<DeliveryManVO> list = JSONArray.toList(json, DeliveryManVO.class);
			if (list.size() == this.deliveryManService.deleteData(list)) {
				j.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 *
	 * @Title: open
	 * @Description: 启用人员
	 * @author 孟凡岭
	 * @param @param ids
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "open")
	@ResponseBody
	public AjaxJson open(@RequestParam(value = "ids[]", required = false) List<String> ids, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			this.deliveryManService.updateStatus(ids, 1);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}

	/**
	 *
	 * @Title: stop
	 * @Description: 停用人员，同时将关联的用户停用
	 * @author 孟凡岭
	 * @param @param ids
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "stop")
	@ResponseBody
	public AjaxJson stop(@RequestParam(value = "ids[]", required = false) List<String> ids, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			this.deliveryManService.updateStatus(ids, 2);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}

	/**
	 * 获取用户信息，获取没有关联人员的
	 *
	 * @param request
	 *            请求
	 * @return 用户信息
	 */
	@RequestMapping(params = "users")
	@ResponseBody
	public List<Map<String, Object>> getUserList(HttpServletRequest request) {
		try {
			return this.deliveryManService.userList();
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}

	/**
	 *
	 * @Title: getUsersAndSelf
	 * @Description: 获取用户信息，获取没有关联人员的和人员自己联系的用户
	 * @author 孟凡岭
	 * @param @param userId
	 * @param @param request
	 * @param @return
	 * @return List<Map<String,Object>> 返回类型
	 * @throws
	 */
	@RequestMapping(params = "getUsersAndSelf")
	@ResponseBody
	public List<Map<String, Object>> getUsersAndSelf(String userId, HttpServletRequest request) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = this.deliveryManService.userList();
			if (StringUtils.isNotBlank(userId)) {
				Map<String, Object> map = this.deliveryManService.getUsersAndSelf(userId);
				list.add(0, map);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return list;
		}
	}

	/**
	 *
	 * @Title: checkUserRelevance
	 * @Description: 判断用户是否已经有关联
	 * @author 孟凡岭
	 * @param @param userId 用户ID
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "checkUserRelevance")
	@ResponseBody
	public AjaxJson checkUserRelevance(String userId, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		boolean flag = this.deliveryManService.checkUserRelevance(userId);
		j.setSuccess(flag);
		j.setMsg("该用户已经关联了业务员，请选择其他用户!");
		return j;
	}

	/**
	 *
	 * @Title: chekcDelCertificates
	 * @Description: 判断证件编号是否已经存在
	 * @author 孟凡岭
	 * @param @param delCertificates
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "chekcDelCertificates")
	@ResponseBody
	public AjaxJson chekcDelCertificates(String delCertificates, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		boolean flag = this.deliveryManService.chekcDelCertificates(delCertificates);
		j.setSuccess(flag);
		return j;
	}

	/**
	 *
	 * @Title: imgUp
	 * @Description: 图片上传返回图片路径
	 * @author 孟凡岭
	 * @param @return
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping(params = "imgUp")
	@ResponseBody
	public AjaxJson imgUp(@RequestParam MultipartFile image, HttpServletRequest request) throws Exception {
		AjaxJson j = new AjaxJson();
		long size = image.getSize();
		if (size > 307200) {
			j.setSuccess(false);
			j.setMsg("照片大小不得超过300KB！");
			return j;
		}
		String path = "upload/deliveryManImg";
//		String imgPath = ImageCompressUtil.saveMinPhoto(image.getInputStream(), path, image.getOriginalFilename(), 139, 1, request).replace("\\", "/");
//		j.setMsg(imgPath);
		j.setSuccess(true);
		return j;
	}

	/**
	 *
	 * @Title: getOpenDeliveryMan
	 * @Description: 获取启用中的配送人员
	 * @author 孟凡岭
	 * @param @return
	 * @return List<Map<String,Object>> 返回类型
	 * @throws
	 */
	@RequestMapping(params = "getOpenDeliveryMan")
	@ResponseBody
	public List<Map<String, Object>> getOpenDeliveryMan(HttpServletRequest request) {
		return this.deliveryManService.getOpenDeliveryMan();
	}

	/**
	 *
	 * @Title: getOpenDeliveryMan
	 * @Description: 获取启用中并且可以接单的配送人员
	 * @author 孟凡岭
	 * @param @return
	 * @return List<Map<String,Object>> 返回类型
	 * @throws
	 */
	@RequestMapping(params = "getOpenAndReceiveDeliveryMan")
	@ResponseBody
	public List<Map<String, Object>> getOpenAndReceiveDeliveryMan(HttpServletRequest request) {
		return this.deliveryManService.getOpenAndReceiveDeliveryMan();
	}
}
